package com.smh.a06exerciseDemo2;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test7 {
    public static void main(String[] args) {

        //获取文件中各种类型文件的数量
        File file = new File("day25-code\\src\\com\\smh\\File");
        HashMap<String, Integer> method = method(file);
        System.out.println(method);

    }

    public static HashMap<String, Integer> method(File file) {
        HashMap<String, Integer> hm = new HashMap<>();
        if (file.isFile()) {
            String name = file.getName();
            String[] split = name.split("\\.");
            if (split.length >= 2) {
                String s = split[split.length - 1];
                if (hm.containsKey(s)) {
                    hm.put(s, hm.get(s) + 1);
                } else {
                    hm.put(s, 1);
                }
            }
            return hm;
        } else {
            File[] files = file.listFiles();
            if (files != null) {
                for (File file1 : files) {
                    HashMap<String, Integer> newHm = method(file1);
                    Set<Map.Entry<String, Integer>> entries = newHm.entrySet();
                    for (Map.Entry<String, Integer> entry : entries) {
                        String key = entry.getKey();
                        Integer value = entry.getValue();
                        if (hm.containsKey(key)) {
                            hm.put(key, hm.get(key) + value);
                        } else {
                            hm.put(key, value);
                        }
                    }
                }
            }
            return hm;
        }
    }
}
